// pages/bleBack/bleBack.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hospitalName: '中日友好医院',
    hospitalId: 50,
    pageNum : 1,
    backList : []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      nowDate: this.getNowDate(new Date()),
      startTime: this.getNowDate(new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 3)),
      scrollHeight:app.getScrollHeight(115)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.changedLocationInfo){
      this.setData({
        hospitalId: this.data.changedLocationInfo.hospitalId,
        hospitalName: this.data.changedLocationInfo.hospitalName,
        pageNum : 1,
      })
    }
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getList(){
    let that = this;
    lbbService.broadcast({ hospitalId: this.data.hospitalId, startTime: this.data.startTime, endTime: this.data.endTime ? this.data.endTime : this.data.nowDate, pageNum: this.data.pageNum }, (res) => {
      if (that.data.pageNum == 1){
        that.setData({
          backList: res.data.broadcastInfoList,
          num: res.data.count
        })
      }else{
        that.setData({
          backList: that.data.backList.concat(res.data.broadcastInfoList),
          num: res.data.count
        })
      }
    })
  },
  changeHos(){
    wx.navigateTo({
      url: '/pages/chooseHospital/chooseHospital',
    })
  },
  getNowDate(nowTime) {
    let reg = /\//g;
    let nowDate = util.formatTime(nowTime).substr(0, 10).replace(reg, '-')
    return nowDate
  },
  bindDateChange(e) {
    console.log("输出了e",e)
    this.setData({
      startTime: e.detail.value    })
    this.getList()
  },
  bindEndTimeDate(e) {
    console.log("输出了e", e)
    this.setData({
      endTime: e.detail.value
    })
    this.getList()
  },
  toBroadcastBackDetail: function (e) {
    console.log("输出了e", e)
    let orderCode = e.currentTarget.dataset.ordercode
    console.log("输出了orderCode", orderCode)
    wx.navigateTo({
      url: '/pages/broadcastBackDetailInfo/broadcastBackDetailInfo?orderCode='+orderCode
    })
  },
  getMore: function () {
    let that = this
    that.data.pageNum += 1
    that.getList()
  }
})